<template>
  <div class="dashboard-container flex flex-column">
    <!-- 状态 -->
    <div class="statistics-row">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-card shadow="hover" class="stat-card">
            <div class="stat-icon users">
              <i class="el-icon-user"></i>
            </div>
            <div class="stat-info">
              <div class="stat-title">用户总数</div>
              <div class="stat-value">{{ (statistics.userManageSize || 0) + (statistics.userOperSize || 0) }}</div>
              <div class="stat-desc">管理员 {{ statistics.userManageSize || 0 }} 操作员 {{ statistics.userOperSize || 0 }}</div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card shadow="hover" class="stat-card">
            <div class="stat-icon devices">
              <i class="el-icon-monitor"></i>
            </div>
            <div class="stat-info">
              <div class="stat-title">设备总数</div>
              <div class="stat-value">{{ statistics.deviceSize || 0 }}</div>
              <div class="stat-desc">在线 {{ (deviceStates.allotOnline || 0) + (deviceStates.downOnline || 0) }} 离线 {{
                (deviceStates.allotOffline || 0) + (deviceStates.downOffline || 0) }}</div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card shadow="hover" class="stat-card">
            <div class="stat-icon areas">
              <i class="el-icon-location"></i>
            </div>
            <div class="stat-info">
              <div class="stat-title">区域总数</div>
              <div class="stat-value">{{ statistics.originSize || 0 }}</div>
              <div class="stat-desc">有设备数 {{ Math.floor(statistics.originValid /
                (statistics.originSize == 0 ? 1 : statistics.originSize) * 100) }}%</div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card shadow="hover" class="stat-card">
            <div class="stat-icon logs">
              <i class="el-icon-document"></i>
            </div>
            <div class="stat-info">
              <div class="stat-title">日志总数</div>
              <div class="stat-value">{{ statistics.logSize || 0 }}</div>
              <div class="stat-desc"></div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- Charts Row -->
    <div class="charts-row">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-card shadow="hover" class="chart-card">
            <div slot="header">
              <span>告警增长趋势</span>
            </div>
            <div class="chart-container">
              <div ref="userChart" style="width: 100%; height: 300px"></div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="12">
          <el-card shadow="hover" class="chart-card">
            <div slot="header">
              <span>设备分布</span>
            </div>
            <div class="chart-container">
              <div ref="deviceChart" style="width: 100%; height: 300px"></div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- Recent Activities -->
    <div class="recent-activities">
      <el-card shadow="hover">
        <div slot="header">
          <span>最近警情</span>
        </div>
        <div class="timeline-container">
          <el-timeline>
            <el-timeline-item v-for="(activity, index) in alarmLog" :key="index" :timestamp="activity.opertime"
              type="success">
              {{ activity.content }}
            </el-timeline-item>
          </el-timeline>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import { getHomeInfo } from "@/http/api/index";
export default {
  name: 'Dashboard',
  data() {
    return {
      statistics: {
        userSize: 0,
        deviceSize: 0,
        originSize: 0,
        logSize: 0,
      },
      alarmLog: [],
      alarmYearInfo: [],
      deviceState: [],
      deviceStates: {}
    }
  },
  mounted() {
    this.getInfo();
  },
  methods: {
    getInfo() {
      getHomeInfo().then(res => {
        if (res.code == 200) {
          let alarmLog;
          alarmLog = res.alarmLog.map((item) => {
            return {
              content: item.content,
              opertime: new Date(Number(item.opertime) * 1000).toLocaleString()
            }

          }) || [];
          this.alarmLog = alarmLog;

          this.alarmYearInfo = res.alarmYearInfo;
          this.deviceState = res.deviceState;
          this.deviceStates = res.deviceStates;

          Object.assign(this.statistics, res);

          // 告警增长趋势
          this.initCharts()
        }

      });

    },
    initCharts() {
      // 初始化告警增长趋势图表
      const userChart = echarts.init(this.$refs.userChart);
      let options = {
        grid: {
          top: '5%',    // 顶部留白比例（原默认约20%）
          left: '10%',   // 左侧留白（保持默认或调整）
          right: '10%',   // 右侧留白
          bottom: '15%'  // 底部留白
        },
        tooltip: {
          trigger: 'axis'
        },
        xAxis: {
          type: 'category',
          data: []
        },
        yAxis: {
          type: 'value'
        },
        series: [{
          data: this.alarmYearInfo,
          type: 'line',
          smooth: true,
          areaStyle: {
            opacity: 0.3
          }
        }]
      };
      let xAxis = []
      this.alarmYearInfo.forEach((v, i) => {
        xAxis.push(i + 1 + "月");
      })
      options.xAxis.data = xAxis;
      userChart.setOption(options)

      // 初始化设备分布图表 
      // -- { allotOnline: 0, allotOffline: 0, downOnline: 0, downOffline: 0 }
      const deviceChart = echarts.init(this.$refs.deviceChart)
      deviceChart.setOption({
        tooltip: {
          trigger: 'item'
        },
        legend: {
          orient: 'vertical',
          left: 'left'
        },
        series: [
          {
            type: 'pie',
            radius: '50%',
            // data: [
            //   { value: 1048, name: '在线设备' },
            //   { value: 735, name: '离线设备' },
            //   { value: 580, name: '维护中' },
            //   { value: 484, name: '故障' }
            // ],
            data: this.deviceState,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      })

      // 监听窗口大小变化，重绘图表
      window.addEventListener('resize', () => {
        userChart.resize()
        deviceChart.resize()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard-container {
  height: 100%;
  padding: 20px;
  background-color: #f0f2f5;
  min-height: calc(100vh - 60px);
  overflow-y: auto;

  &::-webkit-scrollbar {
    width: 6px;
  }

  &::-webkit-scrollbar-thumb {
    background-color: #909399;
    border-radius: 3px;
  }

  &::-webkit-scrollbar-track {
    background-color: #f0f2f5;
  }

  .statistics-row {
    height: 170px;
    margin-bottom: 10px;
  }

  .stat-card {
    display: flex;
    align-items: center;
    padding: 20px;
    height: 120px;
    transition: all 0.3s;

    &:hover {
      transform: translateY(-5px);
    }

    .stat-icon {
      width: 64px;
      height: 64px;
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      margin-right: 15px;

      i {
        font-size: 30px;
        color: #fff;
      }

      &.users {
        background: linear-gradient(45deg, #409EFF, #36D1DC);
      }

      &.devices {
        background: linear-gradient(45deg, #FF9A9E, #FAD0C4);
      }

      &.areas {
        background: linear-gradient(45deg, #A8EDEA, #FED6E3);
      }

      &.logs {
        background: linear-gradient(45deg, #84FAB0, #8FD3F4);
      }
    }

    .stat-info {
      flex: 1;

      .stat-title {
        font-size: 14px;
        color: #909399;
        margin-bottom: 8px;
      }

      .stat-value {
        font-size: 24px;
        font-weight: bold;
        color: #303133;
        margin-bottom: 8px;
      }

      .stat-desc {
        font-size: 14px;
        color: #67C23A;
      }
    }
  }

  .charts-row {
    margin-bottom: 10px;
  }

  .recent-activities {
    flex: 1;
    padding-bottom: 20px;

    .timeline-container {
      height: 250px;
      overflow-y: auto;
      padding: 0 20px 10px 10px;

      &::-webkit-scrollbar {
        width: 6px;
      }

      &::-webkit-scrollbar-thumb {
        background-color: #909399;
        border-radius: 3px;
      }

      &::-webkit-scrollbar-track {
        background-color: #f0f2f5;
      }
    }
  }
}
</style>
